<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>父子组件的数据传递</title>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div id="root">
        <!-- 没有冒号为字符串 -->
        <counter :count="0" @inc="incClick"></counter>
        <!-- 有冒号为数值 -->
        <counter :count="0" @inc="incClick"></counter>
        <div>{{tatol}}</div>
    </div>
    <script>
        // 注册局部组件
        var counter = {
            // 使用props来接收父组件传递的值
            props: ['count'],
            data: function(){
                return {
                    number: this.count,
                }
            },
            template: '<div @click="handleClick">{{number}}</div>',
            methods: {
                handleClick: function(){
                    this.number += 2;
                    this.$emit('inc', 2);
                }
            }
        }
        var vm = new Vue({
            el: "#root",
            data: {
                tatol: 5,
            },
            components: {
                // 在父组件中使用子组件
                counter: counter
            },
            methods: {
                incClick: function(step){
                    // step就是this.$emit('inc', 2)中的参数2
                    this.tatol += step;
                }
            }
        })
    </script>
</body>
</html>